package v1

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"os"
	"strconv"
	"zimuzu_cvn_web_api/config"
	"zimuzu_cvn_web_api/model"
	. "zimuzu_cvn_web_api/pkg/e"
	"zimuzu_cvn_web_api/pkg/errno"
	"zimuzu_cvn_web_api/router/api"
	"zimuzu_cvn_web_api/router/api/app/middleware"
	"zimuzu_cvn_web_api/service"
)

type NavigationAPI struct {
}

func (navigation NavigationAPI) Name() string {
	return "discover API"
}

func (navigation NavigationAPI) Bind() {
	discoverAPI := config.C.Router.Group("/v1/app/navigation", middleware.TrackLog(), middleware.AuthMiddleware())
	{
		discoverAPI.GET("lideshow", slideShow)
		discoverAPI.GET("list", list)
	}
}

type slideShowResponse struct {
	CycleTime int               `json:"cycleTime"`
	Images    []model.Slideshow `json:"imgs"`
}

//slideShow 获取轮播图
//Auth: Xu
func slideShow(c *gin.Context) {
	slideShowList, err := service.GetSlideshow(model.AppBannerSlideshowModule)
	if err != nil {
		api.SendResponse(c, err, slideShowList)
		return
	}
	slideShowResponseData := slideShowResponse{
		CycleTime: service.SlideshowCycleTime,
		Images:    slideShowList,
	}
	errno.OK.Info = "查询成功"
	api.SendResponse(c, errno.OK, &slideShowResponseData)
	return
}

//获取商城链接
//Auth: Xu
type NavigationList struct {
	Id    int              `json:"id"`
	List  []NavigationInfo `json:"list"`
	Title string           `json:"title"`
}

type NavigationInfo struct {
	Id     int    `json:"id"`
	Pic    string `json:"pic"`
	Status int    `json:"status"`
	Title  string `json:"title"`
	Url    string `json:"url"`
}

func list(c *gin.Context) {
	uid, _ := service.GetUserIdByRequest(c)
	userInfo, err := service.GetUserInfo(uid)
	if err != nil {
		api.SendResponse(c, err, nil)
		return
	}

	var re []NavigationList

	filePtr, err := os.Open("conf/navigationList.json")
	if err != nil {
		Log("err", "Open file failed", err)
		api.SendResponse(c, err, nil)
		return
	}
	defer filePtr.Close()

	decoder := json.NewDecoder(filePtr)
	err = decoder.Decode(&re)
	if err != nil {
		Log("err", "Decoder failed", err)
		api.SendResponse(c, err, nil)
		return
	}

	LoginToken, _ := service.GetLoginTokenCache(uid, service.LoginPlatformApp)
	blackUrl := config.C.WalletH5Domain + "/blankPage.html#/"
	//内部用户使用的
	if isInsider := service.IsInsider(uid); isInsider {
		pic := "2_100_s.png"
		url := config.C.WalletH5Domain + "/dataLookBoard.html#/?uid=" + strconv.FormatInt(uid, 10) + "&token=" + LoginToken
		dataView := NavigationInfo{
			Id:     100,
			Url:    url,
			Pic:    pic,
			Title:  "数据看板",
			Status: 1,
		}
		re[1].List = append(re[1].List, dataView)
	}
	for k, v := range re {
		for subK, subV := range v.List {
			re[k].List[subK].Pic = config.C.Domains.YqbAppMenu + subV.Pic

			switch v.Id {
			case 1:
				switch subV.Id {
				case 1:
					//会员购买
					if service.IsInDevelopment(uid) {
						re[k].List[subK].Url = blackUrl
					} else {
						re[k].List[subK].Url = config.C.WalletH5Domain + "/dealerRank.html#/?uid=" + strconv.FormatInt(uid, 10) + "&token=" + LoginToken
					}
				case 2:
					//影视求档
				case 3:
					//字幕组打赏
				case 4:
					//会员经销商
					if service.IsInDevelopment(uid) {
						re[k].List[subK].Url = blackUrl
					} else {
						info, err := model.GetAgentInfoByUid(uid)
						if err != nil {
							continue
						}
						if info.Level == 0 || info.Status != 1 {
							//非经销商
							re[k].List[subK].Url = config.C.WalletH5Domain + "/dealer.html#/?uid=" + strconv.FormatInt(uid, 10) + "&token=" + LoginToken
						} else {
							//经销商
							re[k].List[subK].Url = config.C.WalletH5Domain + "/dealer.html#/dealer/?uid=" + strconv.FormatInt(uid, 10) + "&token=" + LoginToken
						}
					}
				}
			case 2:
				switch subV.Id {
				case 1:
					//商城
					shopToken := service.GetShopTokenCache(uid)
					re[k].List[subK].Url = config.C.WalletH5Domain + "/shop.html#/goodsList?uid=" + strconv.FormatInt(uid, 10) + "&token=" + LoginToken + "&shopToken=" + shopToken +
						"&phone=" + userInfo.PhoneNumber + "&area=" + strconv.Itoa(userInfo.PhoneArea)
				case 2:
					//路由经销商
				case 3:
					//令牌管理
					randNum := strconv.Itoa(RandInt(1, 10000))
					re[k].List[subK].Url = config.C.WalletH5Domain + "/token.html#/manage?uid=" + strconv.FormatInt(uid, 10) + "&token=" + LoginToken + "&rand=" + randNum
				case 4:
					//影视投资
				case 5:
					//钱包会员
				case 6:
					//开发者计划
				case 7:
					//超级节点
					isSuperNode, err := model.CheckUserIsSuperNode(uid)
					if err != nil {
						continue
					}
					if isSuperNode {
						//超级节点用户
						re[k].List[subK].Url = config.C.WalletH5Domain + "/node.html#/superPointUser?uid=" + strconv.FormatInt(uid, 10) + "&token=" + LoginToken +
							"&phone=" + userInfo.PhoneNumber + "&area=" + strconv.Itoa(userInfo.PhoneArea)
					} else {
						//普通用户
						re[k].List[subK].Url = config.C.WalletH5Domain + "/node.html#/?uid=" + strconv.FormatInt(uid, 10) + "&token=" + LoginToken +
							"&phone=" + userInfo.PhoneNumber + "&area=" + strconv.Itoa(userInfo.PhoneArea)
					}
				case 8:
					// 挖矿看板 miningBoard
					minerExist := model.CheckMinerLiveExist(uid)
					if !minerExist {
						// 没有填写超级结点邀请码
						re[k].List[subK].Url = config.C.WalletH5Domain + "/nodeDeposit.html#/?uid=" + strconv.FormatInt(uid, 10) + "&token=" + LoginToken
					} else {
						re[k].List[subK].Url = config.C.WalletH5Domain + "/newMining.html#/?uid=" + strconv.FormatInt(uid, 10) + "&token=" + LoginToken
					}
				case 9:
					// 兑换YTA miningBoard
					re[k].List[subK].Url = config.C.WalletH5Domain + "/yta.html#/?uid=" + strconv.FormatInt(uid, 10) + "&token=" + LoginToken
				}
			case 3:
				switch subV.Id {
				case 1:
					//定期理财
				}
			}
		}
	}

	errno.OK.Info = "成功"
	api.SendResponse(c, errno.OK, re)
	return
}
